// pages/spray/devicedetail.js
const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    deviceId : 0,
    deviceInfo : {},
    errorInfo : {},
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin,
      deviceId: options.deviceId || 0
    });
    this.initDetailData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  toPageLog(event) {
    let type = event.currentTarget.dataset.icontype || '';
    wx.navigateTo({
      url: '/pages/spray/deviceLog?icontype=' + type + "&deviceId=" + this.data.deviceId,
    })
  },
  initDetailData : function() {
    let that = this;
    let solenoidValve = { //电池阀
      errorSum: "-",
      icon: "/images/Frame1.png",
      normalSum: "-",
      warnSum: "-",
      type : "dianshifa",
      title : "电池阀"
    }, electric = { //电源
      errorSum: "-",
      icon: "/images/Frame2.png",
      normalSum: "-",
      warnSum: "-",
      type : "dianyuan",
      title : '电源'
    }, comm = { //通讯模块
      errorSum: "-",
      icon: "/images/Frame3.png",
      normalSum: "-",
      warnSum: "-",
      type : "tongxun",
      title : '通讯模块'
    }, sensor = {
      errorSum: "-",
      icon: "/images/Frame4.png",
      normalSum: "-",
      warnSum: "-",
      type : "qiti",
      title : '感应模块'
    };
    let errorlist = [
      solenoidValve,
      electric,
      comm
    ];
    that.setData({
      dataList : errorlist
    });
    let beginTime = utils.getDay(-3) + " 00:00:00", endTime = utils.getDay(0) + " 23:59:59";
    let p1 = utils.promiseGet('/wx/device/spray/getDeviceInfoById', {"deviceId" : this.data.deviceId}),
      p2 = utils.promiseGet('/wx/device/spray/getErrorInfoByDeviceId', {"deviceId" : this.data.deviceId, "beginTime" : beginTime, "endTime" : endTime});
    Promise.all([p1, p2]).then((result) => {
      console.log(result);
      //p1
      if (result && result[0] && result[0].code == 200) {
        let deviceInfo = result[0].data || {};
        if (deviceInfo.installInduce) {
          errorlist.push(sensor);
        }
        that.setData({
          deviceInfo : deviceInfo
        });
      }

      //p2
      if (result && result[1] && result[1].code == 200) {
        let errorInfo = result[1].data || {};
        solenoidValve.errorSum = errorInfo.solenoidValveFailCount || 0;
        electric.errorSum = errorInfo.electricFailCount || 0;
        comm.errorSum = errorInfo.followComExceptionCount || 0;
        sensor.errorSum = errorInfo.sensorFailCount || 0;
        that.setData({
          errorInfo : errorInfo,
          dataList : errorlist
        });
      }
      


    }).catch(function(e){
        console.log(e);
    });
    
    
    
  },
})